<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>视频录制</title>
  <style>
    * {
      margin: 20px;
      box-sizing: border-box;
    }

    #canvas {
      display: none;
    }

    #video {
      width: 640px;
      height: 360px;
      background: deepskyblue;
      display: block;
      border-radius: 2px;
      box-shadow: 0 1px 1.5px 1px rgba(0, 0, 0, 0.12);
    }

    #button-start {
      width: 300px;
      height: 75px;
      box-sizing: border-box;
      background: linear-gradient(#FF000011, #FF000044) orange;
      color: white;
      font-size: 16px;
      font-family: "monospace";
      border: none;
      outline: none;
      border-radius: 2px;
      box-shadow: 0 1px 1.5px 1px rgba(0, 0, 0, 0.12);
      user-select: none;
    }

    #button-start:hover {
      background: linear-gradient(#FF000033, #FF000077) orange;
    }

    #button-start:active {
      background: linear-gradient(#FF000055, #FF000099) orange;
    }

    #button-start[disabled] {
      background: gray;
    }

    #button-stop {
      width: 300px;
      height: 75px;
      box-sizing: border-box;
      background: linear-gradient(#9933FF00, #9933FF33) dodgerblue;
      color: white;
      font-size: 16px;
      font-family: "monospace";
      border: none;
      outline: none;
      border-radius: 2px;
      box-shadow: 0 1px 1.5px 1px rgba(0, 0, 0, 0.12);
      user-select: none;
    }

    #button-stop:hover {
      background: linear-gradient(#9933FF22, #9933FF66) dodgerblue;
    }

    #button-stop:active {
      background: linear-gradient(#9933FF44, #9933FF88) dodgerblue;
    }

    #button-stop[disabled] {
      background: gray;
    }
  </style>
</head>
<body>
<video id="video" autoplay loop muted></video>
<video id="recorder" autoplay loop muted></video>
<button id="button-start" >开始录制</button>
<button id="button-stop" disabled>结束录制</button>
<script>
  let $video = document.querySelector("#video");
  let $recorder = document.querySelector("#recorder");
  let startButton = document.querySelector("#button-start");
  let stopButton = document.querySelector("#button-stop");
  const recordOption = { mimeType: "video/webm;codecs=vp8" };
  let recorder = null;
  let recordStream = null;
  let chunks = [];
  let blob

  navigator.mediaDevices.getUserMedia({ video: true, audio: false })
    .then((stream) => {
      $video.srcObject = stream;
      recordStream = stream;
    });

  // 按钮事件
  startButton.onclick = e => {
    startButton.disabled = true;
    stopButton.disabled = false;

    recorder = new MediaRecorder(recordStream, recordOption);

    // 收集录制数据
    recorder.ondataavailable = e => {
      chunks.push(e.data);
    };

    recorder.start(10);
  };

  stopButton.onclick = e => {
    startButton.disabled = false;
    stopButton.disabled = true;
    // recorder.stop();

    download();
  };

  // 下载录制内容
  function download() {
    blob = new Blob(chunks, { type: 'video/webm; codecs=vp8'});

    window.URL.revokeObjectURL(blob);
    $recorder.src = window.URL.createObjectURL(blob);
  }
</script>
</body>
</html>
